define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Logistics, GoDialog, GoPage, $timeout) {
		var id = new Array();
		$scope.comp = new Array();
		$scope.input = new Object();
		$scope.input.nPage = 1;
		//页面激活
		$scope.$on('$ionicView.afterEnter', function() {
			GoPage.init_top($scope, 0, 0);
			$scope.init_data(); //必须在定义之后
		}, false);
		$scope.isLoad = false;
		$scope.init_data = function() {
			Logistics.address_list(1, function(data) {
				if(data != null)
					$scope.datas = data;
				else {
					$scope.datas = new Object();
					GoDialog.toast('您不是卖家');
				}

				$scope.input.nPage = 1;
//				console.log(data);
			})
		}

		$scope.choose = function() {
			for(var i = 0; i < $scope.datas.logisticscomp.length; i++) {
				if($scope.datas.logisticscomp[i].choosen_flg == true) {
					$scope.trans_data($scope.datas.logisticscomp[i].id);
				}
			}
		}
		$scope.choose_count = function(data) {
			var n = 0;
			for(var i = 0; i < $scope.datas.logisticscomp.length; i++) {
				if($scope.datas.logisticscomp[i].choosen_flg == true) {
					n = n + 1;
				}
			}
			if(n == 6) {
				data.choosen_flg = false;
				GoDialog.toast('最多选择5个！');
			}
		}
		//转数组
		$scope.trans_data = function(data) {
			var i = $scope.comp.length;
//			console.log(i);
			$scope.comp[i] = {
				"id": data
			};
		}
		//
		$scope.submit_data = function() {
			$scope.choose();
			if($scope.comp.length > 0) {
				Logistics.submit_data($scope.comp, function(data) {
//					console.log(data);
					if(data.success == true) {
						GoDialog.toast('提交成功');
						$scope.comp = new Array();
						GoPage.go_states("t-set"); //提交成功跳转
					} else {
						GoDialog.toast(data.msg);
						$scope.comp = new Array();
					}

				});

			} else {
				GoDialog.toast('请选择物流公司');
			}

		}
		$scope.doRefresh = function() {
			$scope.isLoad = true;
			Logistics.address_list(1, function(data) {
				if(data != null)
					$scope.datas = data;
				else
					$scope.datas = new Object();
				$scope.input.nPage = 1;

			}, function() {
				$scope.$broadcast('scroll.refreshComplete');
				$timeout(function() {
					$scope.isLoad = false;
				}, 1500);
			})
		}
		$scope.doRefreshMore = function() {
//			console.log($scope.isLoad);
			$scope.isLoad = true;
			if($scope.datas.logisticscomp == undefined)
				$scope.input.nPage = 0;
			else
			if($scope.datas.page.count <= $scope.datas.logisticscomp.length) {
				$scope.$broadcast('scroll.infiniteScrollComplete');
				//				$timeout(function() {
				//					$scope.isLoad = false; //可以加载
				//				}, 1000);
			}
			Logistics.address_list($scope.input.nPage + 1, function(data) {
				if(data != null) {
					if($scope.datas.logisticscomp != undefined) {
						$scope.datas.logisticscomp = $scope.datas.logisticscomp.concat(data.logisticscomp);
						$scope.input.nPage += 1;
					} else {
						$scope.datas = data;
						$scope.input.nPage = 1;
					}
				}
			}, function() {
				$scope.$broadcast('scroll.infiniteScrollComplete');
				$timeout(function() {
					$scope.isLoad = false;
				}, 1500);
			})
		}
		//		$scope.init_data();
	}

	controller.$inject = ['$scope', 'Logistics', 'GoDialog', 'GoPage', '$timeout'];

	return app.registerController("logisticsctrl",controller);
});